using System;
using System.ComponentModel;

using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using System.Windows.Input;
using Microsoft.Practices.Prism.Regions;
using ApplicationHost.Views;
using Microsoft.Practices.Prism.Events;
using Infrastructure.Events;
using Microsoft.Practices.Prism.ViewModel;
using Infrastructure;
using Microsoft.Practices.Unity;
using System.Windows;
using System.Threading;
using Microsoft.Practices.Prism;
using Infrastructure.Helpers;
using Microsoft.Practices.Prism.Modularity;

namespace ApplicationHost.ViewModels
{
    public class ShellViewModel : NotificationObject
    {
        IRegionManager regionManager;
        IEventAggregator eventAggregator;
        IUnityContainer container;
        IModuleManager moduleManager;

        Uri view;
        Uri view2;
        bool isBusy = false;

        public ShellViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container, IModuleManager moduleManager)
        {
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
            this.container = container;
            this.moduleManager = moduleManager;
            // Initialize this ViewModel's commands.
            container.RegisterType<Object, DockView>("DockView");
            container.RegisterType<Object, MainPage>("MainPage");
            this.eventAggregator.GetEvent<IsBusyEvent>().Subscribe(ChangeBusy);
            this.eventAggregator.GetEvent<LoginCompletedEvent>().Subscribe(GoToMainPage);
            
        }

        #region Properties
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                RaisePropertyChanged(() => this.IsBusy);
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Change Busy Indicator status
        /// </summary>
        /// <param name="status"></param>
        public void ChangeBusy(bool status)
        {
            this.IsBusy = status;
        }

        public void GoToMainPage(bool status)
        {
            if (PermissionChecker.HasQuyenQuanLyDanhMuc())
            {
            moduleManager.LoadModule("CatalogModule");
            }
            if (PermissionChecker.HasQuyenQuanLyHoSo())
            {
                moduleManager.LoadModule("PortfolioModule");
            }
            if (PermissionChecker.HasQuyenQuanLyHeThong())
            {
            moduleManager.LoadModule("SystemModule");
            }
            
            view = new Uri("DockView", UriKind.Relative);
            this.regionManager.Regions[RegionNames.HostRegion].RequestNavigate(view);

            view2 = new Uri("MainPage", UriKind.Relative);
            this.regionManager.Regions[RegionNames.ContentRegion].RequestNavigate(view2);
        }
        #endregion
    }
}
